package BOOK_describe;

import BOOK_describe.Bookshelves;

import java.util.Arrays;
import java.util.Scanner;

import static java.lang.System.exit;

interface System_function {
    void work(Bookshelves bookshelves,int choice);
}
class Find implements System_function{
    @Override
    public void work(Bookshelves bookshelves, int choice) {
        System.out.print("亲输入你要查找的书: ");
        Scanner scanner = new Scanner(System.in);
        String target = scanner.nextLine();
        int flag = 0;
        for (int i = 0; i < bookshelves.getCount(); i++) {
            if (target.equals(bookshelves.getBookshelves()[i].getBook_name())) {
                System.out.println("找到了 " + bookshelves.getBookshelves()[i].toString());
                flag = 1;
            }
        }
        if (flag == 0) {
            System.out.println("没有欸~");
        }
    }
}
class Exit implements System_function{

    @Override
    public void work(Bookshelves bookshelves, int choice) {
        exit(0);
    }
}
class Add implements System_function{

    static void judge(Bookshelves bookshelves) {
        //要扩容
        if (bookshelves.getCount() == bookshelves.getSpace()) {
            BOOK[] tmp = Arrays.copyOf(bookshelves.getBookshelves(), (bookshelves.getSpace()) * 2);
            bookshelves.setBookshelves_space(tmp,bookshelves.getSpace() * 2);
        }
    }
    @Override
    public void work(Bookshelves bookshelves, int choice) {
        //判断空间是否够
        judge(bookshelves);
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入书名");
        String name = scanner.nextLine();
        System.out.println("输入作者");
        String author = scanner.nextLine();
        System.out.println("输入定价");
        String price = scanner.nextLine();
        BOOK book = new BOOK(name, author, price);
        bookshelves.setBookshelves(book);
        System.out.println("书籍添加成功");
    }
}

class Del implements System_function{

    @Override
    public void work(Bookshelves bookshelves, int choice) {
        System.out.println("请输入你要删除得名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();//要删除得名字
        int pos = 0;
        int flag = 0;
        //找到删除得名字
        for (int i = 0; i < bookshelves.getCount(); i++) {
            if (name.equals(bookshelves.getBookshelves()[i].getBook_name())) {
                pos = i;
                flag = 1;
                break;
            }
        }
        if(flag == 1) {//用后面得覆盖前面的
            for (int i = pos; i < bookshelves.getCount() - 1; i++) {
                bookshelves.getBookshelves()[i] = bookshelves.getBookshelves()[i + 1];
            }
            int count = bookshelves.getCount() - 1;
            bookshelves.setCount(count);
            System.out.println("删除成功");
        }
        else{
            System.out.println("没有这本书");
        }
    }
}

class Show implements System_function{

    @Override
    public void work(Bookshelves bookshelves, int choice) {
        //展示所以书
        for (int i = 0; i < bookshelves.getCount(); i++) {
            System.out.println(bookshelves.getBookshelves()[i].toString());
        }
    }
}
class Borrow implements System_function{

    @Override
    public void work(Bookshelves bookshelves, int choice) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookshelves.getCount(); i++) {
            if (name.equals(bookshelves.getBookshelves()[i].getBook_name())) {
                bookshelves.getBookshelves()[i].setIs_borrow(true);
            }
        }
        System.out.println("成功借出");
    }
}
class Revert implements System_function{

    @Override
    public void work(Bookshelves bookshelves, int choice) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookshelves.getCount(); i++) {
            if (name.equals(bookshelves.getBookshelves()[i].getBook_name())) {
                bookshelves.getBookshelves()[i].setIs_borrow(false);
            }
        }
        System.out.println("成功归还");
    }
}


